package com.kgc.scd.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: WangKuoKuo
 * @create: 2022-06-10 10:13
 * @description:
 **/

public class MD5Util {
    //加密方式
    public static String md5Password(String password) {
        //获取信息摘要器对象
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(password.getBytes());
            StringBuffer buffer = new StringBuffer();

            for (byte b : result) {
                //与运算
                int number = b & 0xff;
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(str);
            }
            //返回标准的md5加密后的结果
            return buffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    //用户名验证   //必须是6-10位字母、数字、下划线（这里字母、数字、下划线是指任意组合，没有必须三类均包含） 不能以数字开头
    public static boolean checkName(String name) {
//        String regExp = "^[^0-9][\\w_]{5,9}$";
        String regExp = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,5})";
        if (name.matches(regExp)) {
            return true;
        } else {
            return false;
        }
    }

    //密码验证  //必须是5-15位的字母、数字、下划线（这里字母、数字、下划线是指任意组合，没有必须三类均包含）
    public static boolean checkPwd(String pwd) {
        String regExp = "(^[a-zA-Z0-9_-]{5,15}$)";
        if (pwd.matches(regExp)) {
            return true;
        }
        return false;
    }

    //手机号校验
    public static boolean checkPhone(String phone) {
        String regExp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        if (phone.matches(regExp)) {
            return true;
        }
        return false;
    }

    //    private static String regex;
    private static Pattern pattern;
    private static Matcher matcher;

    //邮箱校验
    public static boolean checkEmail(String email) {
        String mailRegex, mailName, mailDomain;
        mailName = "^[0-9a-z]+\\w*";
        mailDomain = "([0-9a-z]+\\.)+[0-9a-z]+$";
        mailRegex = mailName + "@" + mailDomain;
        pattern = Pattern.compile(mailRegex);
        matcher = pattern.matcher(email);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }
}

